
from django.template import Context, loader
from django.http import HttpResponse
from gtfs.models import *
import datetime

def index_view(request):
    t = loader.get_template('armada/index.html')
    c = Context({
            'agencies' : Agency.objects.all()
            })
    return HttpResponse(t.render(c))

def agency_view(request, name):
    t = loader.get_template('armada/agency.html')
    c = Context({
            'agency' : Agency.objects.get(name=name)
            })
    return HttpResponse(t.render(c))

def agency_routes_view(request, name):
    t = loader.get_template('armada/agency_routes.html')
    agency = Agency.objects.get(name=name)
    c = Context({
            'agency' : Agency.objects.get(name=name),
            'routes' : agency.route_set.all().order_by('short_name'),
            })
    return HttpResponse(t.render(c))

def route_view(request, agency_name, route_text_id):
    t = loader.get_template('armada/route.html')
    agency = Agency.objects.get(name=agency_name)
    route = Route.objects.get(agency=agency, text_id=route_text_id)
    c = Context({
            'agency' : agency,
            'route' : route,
            })
    return HttpResponse(t.render(c))

def agency_stops_view(request, name):
    t = loader.get_template('armada/agency_stops.html')
    agency = Agency.objects.get(name=name)
    c = Context({
            'agency' : Agency.objects.get(name=name),
            'stops' : agency.dataset.stop_set.all().order_by('name'),
            })
    return HttpResponse(t.render(c))

def stop_view(request, agency_name, stop_code):
    t = loader.get_template('armada/stop.html')
    agency = Agency.objects.get(name=agency_name)
    stop = Stop.objects.get(dataset=agency.dataset, code=stop_code)
    c = Context({
            'agency' : Agency.objects.get(name=agency_name),
            'stop' : Stop.objects.get(dataset=agency.dataset, code=stop_code),
            'timetable' : stop.timetable(datetime.date.today()),
            })
    return HttpResponse(t.render(c))
